<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    <script type="text/javascript">
        window.onload = function() {
            //1.获取a节点并对其添加onclick响应函数
            document.getElementsByTagName("a")[0].onclick = function() {

                //3.创建一个XMLHttpRequest对象
                var request = new XMLHttpRequest();

                //4.准备发送请求的数据：url
                var url = "http://localhost:8080/xmlhttpRequest/post";//不同的时间戳，来达到禁用缓存的目的
                var method = "POST";

                //5.调用XMLHttpRequest 对象的open方法
                request.open(method, url);

                request.setRequestHeader("Content-Type", "application/json");
                //6.调用XMLHttpRequest 对象的send方法
                var param = {
                    "id":"1",
                    "address":"测试地址"
                }
                request.send(JSON.stringify(param));

                //7.为XMLHttpRequest 对象添加onreadystatechange 响应函数
                request.onreadystatechange = function() {
                    //alert(request.readyState);
                    //8.判断响应是否完成：XMLHttpRequest 对象的readyState 属性值为4的时候
                    if (request.readyState == 4) {
                        //9.在判断响应是否可用：XMLHttpRequest 对象status 属性值为200
                        if (request.status == 200 || request.status == 304) {
                            //10.打印响应结果： responseText
                            alert(request.responseText);

                        }

                    }
                }

                //2.取消 a节点的默认行为
                return false;
            }
        }
    </script>
</head>
<body>
<a href="">postDemo</a>
</body>
</html>
